package pl.prv.polanie.client.filemanager;


import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LoadListener;
import com.google.gwt.user.client.ui.MouseListenerAdapter;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Janusz Prokulewicz
 */


public class File extends Composite implements NestedElement {


	private static final String STYLE__FILE = "swo-File";
	private static final String STYLE__FILE_ICON = "swo-File-fileIcon";
	private static final String STYLE__FILE_NAME = "swo-File-fileName";
	
	private static final String STYLE__FILE_HIGHLIGHTED = "swo-File-highlighted";
	
	
	private static final int FILEICON_CONST_IMAGE = 0;
	private static final int FILEICON_CONST_EMPTY = 1;
	
	
	private final HorizontalPanel mainHP = new HorizontalPanel();
	
	private final HTML fileIconHTML = new HTML();
	private final HTML fileNameHTML = new HTML();
	
	private String fileName;
	private String fileDir;
	
	
	private boolean afterOnMouseEnter = false;
	
	
	public File(String fileDir, String fileName) {
		initWidget(mainHP);

		this.fileDir = fileDir;
		this.fileName = fileName;
		
		mainHP.setStyleName(STYLE__FILE);
		
		fileIconHTML.setStyleName(STYLE__FILE_ICON);
		fileIconHTML.setHeight(16+"px");
		fileIconHTML.setWidth(20+"px");
		
		
		String[] ss = fileName.split("[.]");
		String end = ss[ss.length-1];
		
		if (end.equals("png") || end.equals("bmp") || end.equals("jpg") || end.equals("jpeg")) {
//			WidgetsProject.log("obrazek");
			setIcon(FILEICON_CONST_IMAGE);
			addThumbnailListener();
		} else {
			setIcon(FILEICON_CONST_EMPTY);
		}
		
		
		fileNameHTML.setStyleName(STYLE__FILE_NAME);
		fileNameHTML.setHTML("<a href='" + fileDir + fileName + "' title='"+ fileDir + fileName +"' target='top' >" + fileName + "</a>");
		fileNameHTML.setHeight(16+"px");
		
		
		mainHP.add(fileIconHTML);
		mainHP.add(fileNameHTML);
		
		
	}
	
	public void setIcon(int iconConst) {
		String html = "<img src='" + getIconUrl(iconConst) + "' />";
		fileIconHTML.setHTML(html);
	}
	
	public String getIconUrl(int iconConst) {
		String url = "";
		switch (iconConst) {
		case FILEICON_CONST_IMAGE:
			url = "fileicon_image.png";
			break;
		case FILEICON_CONST_EMPTY:
			url = "fileicon_empty.png";
			break;

		default:
			break;
		}
		
		return "swo-images/" + url;
	}
	
	/**
	 * Dodaje listener odpowiedzialny za wyswietlanie tooltipa.
	 */
	private void addThumbnailListener() {
		fileNameHTML.addMouseListener(new MouseListenerAdapter() {
			public void onMouseEnter(Widget sender) {
				
				fileIconHTML.addStyleName(STYLE__FILE_HIGHLIGHTED);
				fileNameHTML.addStyleName(STYLE__FILE_HIGHLIGHTED);

				final PopupPanel pp = new PopupPanel(true, false);
				pp.setStyleName("test");
				
				final Image im = new Image(fileDir + fileName);

				int thumbnailLeft = sender.getAbsoluteLeft() + sender.getOffsetWidth();
				int thumbnailTop = sender.getAbsoluteTop() + sender.getOffsetHeight();
				
				ThumbnailManager.displayThumbnail(pp, thumbnailLeft, thumbnailTop);
				
//				pp.setWidget(im);
				
				final Tooltip t = new Tooltip(new Label("Loading..."));
				
				pp.setWidget(t);
				
//				if (!afterOnMouseEnter) {
				ImageLoader.loadImage(im);
				if (im.getWidth() == 0 || im.getWidth() == 24) {
//					WidgetsProject.log("szerokosc obrazka 0 lub miniaturka");
					im.addLoadListener(new LoadListener() {
						public void onLoad(final Widget sender) {
//							WidgetsProject.log("onLoad");
							if (pp.isAttached()) {
//								WidgetsProject.log("attached");
								
//								WidgetsProject.log("w: " + im.getWidth() + ", h: " + im.getHeight());
								
								int w = im.getWidth();
								int h = im.getHeight();
								
								float zoomRate;
								
								if (w > h) {
									zoomRate = w/(float)200;
								} else {
									zoomRate = h/(float)200;
								}
								
								
								im.setWidth(w/zoomRate+"px");
//								WidgetsProject.log("ustawiam szer " + (w/zoomRate)+"px");
								im.setHeight(h/zoomRate+"px");
//								WidgetsProject.log("ustawiam wys " + (h/zoomRate)+"px");
//								WidgetsProject.log("lewo " + (sender.getAbsoluteLeft() + sender.getOffsetWidth()));
//								pp.setWidget(im);
								t.setWidgetInTooltip(im);

							}
						}
						public void onError(Widget sender) {
						}
					});
				} else {
//					WidgetsProject.log("obrazek wczytany, szerokosc: " + im.getWidth());
					
					int w = im.getWidth();
					int h = im.getHeight();
					
					float zoomRate;
					
					if (w > h) {
						zoomRate = w/(float)200;
					} else {
						zoomRate = h/(float)200;
					}
					im.setWidth(w/zoomRate+"px");
//					WidgetsProject.log("ustawiam szer " + (w/zoomRate)+"px");
					im.setHeight(h/zoomRate+"px");
//					WidgetsProject.log("ustawiam wys " + (h/zoomRate)+"px");
					
					t.setWidgetInTooltip(im);
//					pp.setWidget(im);

//					pp.center();
				}
				
				
				
			}
			
			
			public void onMouseLeave(Widget sender) {
				fileIconHTML.removeStyleName(STYLE__FILE_HIGHLIGHTED);
				fileNameHTML.removeStyleName(STYLE__FILE_HIGHLIGHTED);
				ThumbnailManager.hideThumbnail();
			}
		});
		
		
		
//		DOM.sinkEvents(fileNameHTML.getElement(), Event.MOUSEEVENTS);
////		sinkEvents(Event.ONMOUSEOVER|Event.ONMOUSEOUT);
//		DOM.setEventListener(fileNameHTML.getElement(), new EventListener() {
//			public void onBrowserEvent(Event event) {
//				WidgetsProject.log("OVER");
//				switch (DOM.eventGetType(event)) {
//				case Event.ONMOUSEMOVE:
//					WidgetsProject.log("OVER");
//					PopupPanel pp = new PopupPanel(true, true);
//					pp.setWidget(new Label("asasdsd"));
//					pp.center();
//					break;
//				default:
//					break;
//				}	
//				
//			}
//		});
		
		
	}

}
